<?php

namespace backend\models;

use common\server\BasicDataServer;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\HouseModel;
use common\models\BaseRegionModel;
/**
 * SearchHouseModel represents the model behind the search form about `common\models\HouseModel`.
 */
class SearchHouseModel extends HouseModel
{
    public  $mode = 0;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'decoration_id', 'total_price', 'region_id', 'room1', 'room2'], 'integer'],
            [['house_name', 'return_money', 'developer', 'checkout', 'property', 'house_area', 'address', 'head', 'phone','mode'], 'safe'],
            [['house_unit_price'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = HouseModel::find();
        $query->orderBy(['id'=>SORT_DESC]);
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        switch ($this->mode){
            case 1:
                $query->where(['is_banner'=>1]);
                break;
            case 2:
                $query->where(['is_adv' => 1]);
                break;
            case 3:
                $query->where(['is_banner_home'=>1]);
                break;
            case 4:
                $query->where(['is_recommend'=>1]);
                break;

        }

        $query->andFilterWhere(['like', 'house_name', $this->house_name]);
        if(!empty($this->region_id)){
            // $selectArr = BasicDataServer::instance()->getChildRegion($this->region_id);
            // array_push($selectArr,['id'=>$this->region_id]);
            $rr = BaseRegionModel::find()->select(['id'])->where(['parent'=>$this->region_id])->asArray()->column();
            array_push($rr,$this->region_id);
            $query->andWhere(['in','region_id',$rr]);

            // $query->andWhere(['region_id'=> $selectArr]);
        }
        return $dataProvider;
    }

    public function getModes(){
        return [0=>'全部', 1=>'列表banner',3=>'首页banner' , 2=>'弹出广告','4'=>'推荐楼盘'];
    }
}
